package com.intelligentmeterreadingsys.waterlist.reviewdata.activity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.httpserver.RequestUrl;
import com.intelligentmeterreadingsys.httpserver.UploadHttpClient;
import com.intelligentmeterreadingsys.povo.WaterInfoVO;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterlist.copywater.db.CopyWaterDao;
import com.intelligentmeterreadingsys.waterlist.reviewdata.db.ReviewDataDao;
import dbhelper.XmlNameHelper;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 复查数据上传
 * @author king
 *
 */
public class ReviewDataActivityUpload extends Activity{
      public Button button;//上传按钮
	 
	 public TextView textview1;//需上传总数
	 
	 public TextView textview2;//未上传总数
	 
	 public TextView textview3;//进度条描述
	 
	 public TextView textview4;
	 
	 public ProgressBar pb;//进度条
	 public List<WaterInfoVO> list=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.waterbanding_onetoupload);
		init();
	}

	/**
	 * 初始化控件、数据
	 */
	public void init(){		
		   button=(Button)findViewById(R.id.waterbanding_upload_button);
		   pb=(ProgressBar)findViewById(R.id.waterbanding_progressBar);
		   textview1=(TextView)findViewById(R.id.waterbanding_upload_total);
		   textview2=(TextView)findViewById(R.id.waterbanding_noupload_total);
		   textview3=(TextView)findViewById(R.id.waterbanding_pb_textview);
		   textview4=(TextView)findViewById(R.id.onekeyupload_info);
		   textview4.setText("复抄数据一键上传");
		   ReviewDataDao dao=new ReviewDataDao(this);
		   final int noupload=dao.queryNoUpload();
		   textview1.setText(dao.queryReviewNumber()+"");
		   textview2.setText(noupload+"");
		   textview3.setText("0%");
		   dao.close();
		   button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(noupload<=0){
					Toast.makeText(ReviewDataActivityUpload.this, "数据已经上传字了！", Toast.LENGTH_LONG).show();					
					return;
				}			   
				ReviewDataDao dao=new ReviewDataDao(ReviewDataActivityUpload.this);
				list=dao.queryAllNoUpload(null);			   
				dao.close();				
				textview3.setText("正在上传中0%");
				pb.setMax(list.size());				
				new Thread(){
					@Override
					public void run() {						
						upload();
					}
					
				}.start();
				
			}
		});
	}
	
	/**
	 * 批量上传数据
	 * @return
	 */
	public boolean upload(){		
		RequestUrl r=new RequestUrl(this);
		String url=r.getUrl(RequestUrl.COPYWATERLIST);
		System.out.println(url);
		SharedPreferenceUtil sp=new SharedPreferenceUtil(this);
		String imsi=sp.getValue(XmlNameHelper.IMSI);
		for(int i=0;i<list.size();i++){			
			WaterInfoVO water=list.get(i);
			Map<String, String> map=new HashMap<String, String>();
			map.put("waternumber", water.getWaternumber());
			map.put("thismonthwater", water.getThismonthwater()+"");
			map.put("thisdate",water.getThisdate());
			map.put("usewater", water.getThismonthwater()-water.getLastmonthwater()+"");
			map.put("imsi",imsi);
			map.put("sid", water.getSid());
			map.put("isnorfid", water.getIsNorfid());
			Map<File, String> mapfile=new HashMap<File, String>();
		  Map<String, String> maps=new HashMap<String, String>();
			List<String> lists=list.get(i).getImagepath();
			for(int k=0;k<lists.size();k++){
				File file=new File(lists.get(k));
				if(file.exists()){
					mapfile.put(file, "imagepath");
					maps.put(file.getName(), "imagename");
				}
				
			}
			
		String respon=	UploadHttpClient.uploadSubmit(url, maps, mapfile,map);
		
		if(respon.equals("error")){
			Message msg=myHandler.obtainMessage();
			msg.what=0;
			myHandler.sendMessage(msg);
			return false;

		}
		try {
			JSONObject obj=new JSONObject(respon);
			
			String message=obj.getString("message");
			if(message.equals("yes")){
				Message msg=myHandler.obtainMessage();
				msg.what=1;
				msg.arg1=i+1;
				myHandler.sendMessage(msg);
				CopyWaterDao dao=new CopyWaterDao(this);
				dao.uploadstatus(water.getId());
				
				dao.close();
				
			}else{
				Message msg=myHandler.obtainMessage();
				msg.what=2;
			
				myHandler.sendMessage(msg);
				
				return false;
			}
		} catch (JSONException e) {
			
			Message msg=myHandler.obtainMessage();
			msg.what=2;
			
			myHandler.sendMessage(msg);
			return false;
		}
		
		}
		
		
		return true;
		
	}
	Handler myHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if(msg.what==1){
				pb.setProgress(msg.arg1);
				textview3.setText("正在上传中...."+(float)msg.arg1/(float)list.size()*100+"%");
			  
				if(msg.arg1==list.size()){
					
					textview3.setText("上传完毕100%");
					Toast.makeText(ReviewDataActivityUpload.this, "上传成功", 1000).show();
					 CopyWaterDao dao=new CopyWaterDao(ReviewDataActivityUpload.this);
					   final int noupload=dao.NoUploadWater();
					   textview1.setText(dao.UploadWater()+"");
					   textview2.setText(noupload+"");
					   textview3.setText("0%");
					   dao.close();
					
				}
			}else if(msg.what==2){
				Toast.makeText(ReviewDataActivityUpload.this, "上传失败", 1000).show();
				textview3.setText("0%");
			}else if(msg.what==0){
				Toast.makeText(ReviewDataActivityUpload.this, "网络连接错误", 1000).show();
				textview3.setText("0%");
			}
		}
		
	};
}
